<?PHP
session_start();
?>
<TABLE width="100%" border="0" cellpadding="4" cellspacing="0" style="mainTableBGColor">
	<TR><TD valign="top" align="center"> 
	
<FORM name="form" action="formaction.php" method="post" enctype="multipart/form-data" onKeyPress="swallowEnter()">	
<TABLE border="0" width="625" cellpadding="2" cellspacing="0">
	<TR>
		<TD>
		
		<?PHP
		
		
		
			//Remove Programs from $_SESSION['updateArray'] via user
		 	 if(isset($_GET['dropID'])){
			 
			 
							function removeItemFromArray($index){
						if($index == '0'){//item to remove is at begining of array
							array_shift($_SESSION['updateArray']);
						}else if((count($_SESSION['updateArray']) - $index) == '1'){//item to remove is at end of array
							array_pop($_SESSION['updateArray']);
						}else{
							$tempArray1 = array_slice($_SESSION['updateArray'], 0, $index);
							$tempArray2 = array_slice($_SESSION['updateArray'], ($index + 1));///keep stuff after index position
							$_SESSION['updateArray'] = array_merge($tempArray1,$tempArray2);
						}		
								
						/* if(count($_SESSION['updateArray']) == '0'){
							$_SESSION['displayConflicts'] = array();//emopty out array from form action
						} */
					}//end function
			 
			 
			 	removeItemFromArray($_GET['dropID']);
			 
			 }
		 
		
		
		
		
		
		
		
		if(!isset($_GET['programID']) && count($_SESSION['updateArray']) == '0'){
		
				echo("<script language=\"JavaScript\" type=\"text/javascript\"> 
				function launch( form ) { 
					var newIndex = form.name.selectedIndex; 
					if ( newIndex == 0 ) { 
						alert( \"Please select a program!\" ); 
					} else { 
						cururl = \"index.php?ID=1&subMenu=0&action=modifyPrograms&programID=\" + document.form.name.value; 
						self.location=cururl;
		 
					} 
				} 
				</script> ");
				
				echo("<TR><TD valign=\"top\" align=\"center\">\n");
				
				echo("<select name=\"name\" size=\"1\" onChange=\"launch( this.form )\"><option value=\"not_selected\" selected>Choose a program to modify:</option>\n");
					
				
				$Query = "SELECT * FROM $programTitles_Table WHERE title != '&nbsp;' ORDER BY title asc";
				$result = $db->getAll($Query, DB_FETCHMODE_ASSOC);
				if (DB::isError($result)) {
				die ($result->getMessage());
				}		
				
				for ($i = 0; $i < count($result); $i++) {
				$ID = $result[$i]['ID'];
				$name = stripslashes($result[$i]['title']);  
				echo("<option value=\"$ID\">$name</option>\n"); 
				}
				
				echo("</select>\n");
		 
		 }else{
		 
		 
//Check to make sure all fields are filled out		
if(!isset($_SESSION['updateArray'])){
	//$_SESSION['updateArray'] = '1';
} 
 echo"
<script language=\"JavaScript\">
<!--
function formCheck(){\n";
if(!isset($_SESSION['updateArray'])){
echo "var numItems = '1';";
}else{
	echo "var numItems = ".count($_SESSION['updateArray']).";";
}

echo"
var msg = \"\";
for (var q=0; q < numItems; q++) {
	var title = eval(\"document.form.title\"+q+\".value\");
	var hour = eval(\"document.form.hour\"+q+\".value\");
	var minute = eval(\"document.form.minute\"+q+\".value\");
	var seconds = eval(\"document.form.seconds\"+q+\".value\");
	var mon = eval(\"document.form.mon\"+q+\".checked\");
	var tue = eval(\"document.form.tue\"+q+\".checked\");
	var wed = eval(\"document.form.wed\"+q+\".checked\");
	var thu = eval(\"document.form.thu\"+q+\".checked\");
	var fri = eval(\"document.form.fri\"+q+\".checked\");
	var sat = eval(\"document.form.sat\"+q+\".checked\");
	var sun = eval(\"document.form.sun\"+q+\".checked\");
	var originalName = eval(\"document.form.originalName\"+q+\".value\");
	
	//check for required fields:
	if(title == \"\" || hour == \"not_selected\" || minute == \"not_selected\" || seconds == \"not_selected\" || (mon == false && tue == false && wed == false && thu == false && fri == false && sat == false && sun == false)){
		msg = msg + \"Please complete all fields for \" + originalName + \"\\n\";
	}

	}//end loop
	
	if(msg == ''){
		document.form.submit();
	}else{
		alert(msg);
	}
	

}
//-->
</script> 
"; 
 
 
 
		 
			 //FORM TO MODIFY PROGRAMS
			 if(isset($_GET['programID'])){
				$_SESSION['updateArray'][0] = $_GET['programID'];
			 }


			$result = array();
			for ($r = 0; $r < count($_SESSION['updateArray']); $r++) {
				$sth = "SELECT * FROM $programTitles_Table WHERE ID = '".$_SESSION['updateArray'][$r]."'";
				$result[] = $db->getAll($sth,DB_FETCHMODE_ASSOC);
			  } 
			if (DB::isError($result)) {die ($result->getMessage());}
			
			
			for ($w = 0; $w < count($result); $w++) {
			
			
			//BEGIN LOOP 
					echo"<input name=\"ID$w\" type=\"hidden\" value=\"".$result[$w][0]['ID']."\">\n<TABLE border=\"1\" width=\"625\" cellpadding=\"4\" cellspacing=\"0\">
			
			<TR class=\"blockHeader\">
				<TD><input name=\"originalName$w\" type=\"hidden\" value=\"".stripslashes(stripslashes($result[$w][0]['title']))."\">".stripslashes(stripslashes($result[$w][0]['title']))."</TD>
				<TD align=\"right\" class=\"dropButton\"><A HREF=\"index.php?ID=1&subMenu=0&action=modifyPrograms&dropID=$w\">Remove</A></TD>

			</TR>			
			
			<TR class=\"even\">
				<TD class=\"formDescriptiveText\">Program Title:</TD>
				<TD class=\"formElementTextLeft\"><input name=\"title$w\" type=\"text\" size=\"30\" value=\"".stripslashes(stripslashes($result[$w][0]['title'])). "\" onkeypress=\"return swallowEnter(event,this.form)\"></TD>
			</TR>
			
			<TR class=\"odd\">
				<TD class=\"formDescriptiveText\">Air Time:</TD>
				<TD class=\"formElementTextLeft\">";
				
				//echo date("g:i:s a",mktime ((str_replace (":", ",", $result[$w][0]['military'])),0,0,0));
				
				//#####################################
				// Form Item - Hour
				//#####################################
				echo "\n<select name=\"hour$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n";
					echo("<option value=\"not_selected\" selected>hour</option>\n"); 
					
				for ($i = 1; $i < 13; $i++) {
					if($i == date("g",mktime ((str_replace (":", ",", $result[$w][0]['military'])),0,0,0))){
						echo("<option value=\"$i\" selected>$i</option>\n"); 
					}else{
						echo("<option value=\"$i\">$i</option>\n"); 
					}
					
				}
				echo "</select>&nbsp; : &nbsp;\n\n";
				
				
				//#####################################
				// Form Item - Minute
				//#####################################
				echo "\n<select name=\"minute$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n";
					echo("<option value=\"not_selected\" selected>minute</option>\n"); 
				for ($i = 0; $i < 60; $i++) {
					$value = str_pad($i, 2, "0", STR_PAD_LEFT);  
					if($value == substr($result[$w][0]['military'], 3, 2)){
						echo("<option value=\"$value\" selected>$value</option>\n"); 
					}else{
						echo("<option value=\"$value\">$value</option>\n"); 
					}
				}
				echo "</select>&nbsp; : &nbsp;\n\n";
				
				//#####################################
				// Form Item - Seconds
				//#####################################
				echo "\n<select name=\"seconds$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n";
					echo("<option value=\"not_selected\" selected>seconds</option>\n"); 
				for ($i = 0; $i < 60; $i++) {
					$value = str_pad($i, 2, "0", STR_PAD_LEFT);  
					if($value == substr($result[$w][0]['military'], 6, 2)){
						echo("<option value=\"$value\" selected>$value</option>\n"); 
					}else{
						echo("<option value=\"$value\">$value</option>\n"); 
					}
				}
				echo "</select>&nbsp; &nbsp;\n\n";	
				
				//#####################################
				// Form Item - AM / PM
				//#####################################
				echo "\n<select name=\"ampm$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n";
				if(date("a",mktime ((str_replace (":", ",", $result[$w][0]['military'])),0,0,0)) == 'am'){
					echo "<option value=\"AM\" selected>AM</option>";
					echo "<option value=\"PM\">PM</option>";
				}else{
				echo "<option value=\"AM\">AM</option>";
				echo "<option value=\"PM\" selected>PM</option>";				
				}

				echo "</select>&nbsp; &nbsp;\n\n";	
				echo"
				</TD>
			</TR>
			

			<TR class=\"even\">
				<TD class=\"formDescriptiveText\">Start Date:</TD>
				<TD class=\"formElementTextLeft\">";
				
				        //#####################################
						// Form Item - Start Date
						//#####################################
						//Month Drop Down List
						echo("<select name=\"startMonth$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n");
						 echo("<option value=\"not_selected\" selected>month</option>\n");
						for ($i = 1; $i < 13; $i++) {
						$value = str_pad($i, 2, "0", STR_PAD_LEFT);
						
						if($value == date("m",$result[$w][0]['startTimeStamp']) && $result[$w][0]['startTimeStamp'] != "-1"){
						 echo("<option value=\"$value\" selected>". date("F",mktime (0,0,0,$i,1,2005) )."</option>\n");
						}else{
						 echo("<option value=\"$value\">". date("F",mktime (0,0,0,$i,1,2005) )."</option>\n");
						}
						 
						 
						}
						echo("</SELECT>\n\n");
						
						
						//Day Drop Down List
						echo("<select name=\"startDay$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n");
							echo("<option value=\"not_selected\" selected>day</option>\n");
						for ($i = 1; $i < 32; $i++) {
							$value = str_pad($i, 2, "0", STR_PAD_LEFT);
							if($value == date("d",$result[$w][0]['startTimeStamp']) && $result[$w][0]['startTimeStamp'] != "-1"){
								echo("<option value=\"$value\" selected>$value</option>\n");
							}else{
								echo("<option value=\"$value\">$value</option>\n");
							}
						}
						echo("</SELECT>\n\n");
						
						
						//Year Drop Down List
						echo("<select name=\"startYear$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\"> \n");
							echo("<option value=\"not_selected\" selected>year</option>\n");
							for ($i = 0; $i < 3; $i++) {
								if((date("Y") + $i ) == date("Y",$result[$w][0]['startTimeStamp']) && $result[$w][0]['startTimeStamp'] != "-1"){
									echo("<option value=\"" . (date("Y") + $i ). "\" selected>" . (date("Y") + $i ) . "</option>\n");
								}else{
									echo("<option value=\"" . (date("Y") + $i ). "\">" . (date("Y") + $i ) . "</option>\n");
								}
							}
						echo("</SELECT> \n");
				
				
				
				echo"&nbsp; (optional)
				</TD>
			</TR>
			
			<TR class=\"odd\">
				<TD class=\"formDescriptiveText\">End Date:</TD>
				<TD class=\"formElementTextLeft\">";
				
				        //#####################################
						// Form Item - END Date
						//#####################################
						//Month Drop Down List
						echo("<select name=\"endMonth$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n");
						 echo("<option value=\"not_selected\" selected>month</option>\n");
						for ($i = 1; $i < 13; $i++) {
							$value = str_pad($i, 2, "0", STR_PAD_LEFT);
							if($value == date("m",$result[$w][0]['endTimeStamp']) && $result[$w][0]['endTimeStamp'] != "-1"){
							 echo("<option value=\"$value\" selected>". date("F",mktime (0,0,0,$i,1,2005) )."</option>\n");
							}else{
							 echo("<option value=\"$value\">". date("F",mktime (0,0,0,$i,1,2005) )."</option>\n");
							}
						 }
						echo("</SELECT>\n\n");
						
						//Day Drop Down List
						echo("<select name=\"endDay$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\">\n");
							echo("<option value=\"not_selected\" selected>day</option>\n");
						for ($i = 1; $i < 32; $i++) {
							$value = str_pad($i, 2, "0", STR_PAD_LEFT);
							if($value == date("d",$result[$w][0]['endTimeStamp']) && $result[$w][0]['endTimeStamp'] != "-1"){
								echo("<option value=\"$value\" selected>$value</option>\n");
							}else{
								echo("<option value=\"$value\">$value</option>\n");
							}
						}
						echo("</SELECT>\n\n");
						
						
						//Year Drop Down List
						echo("<select name=\"endYear$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\"> \n");
							echo("<option value=\"not_selected\" selected>year</option>\n");
							for ($i = 0; $i < 3; $i++) {
								if((date("Y") + $i ) == date("Y",$result[$w][0]['endTimeStamp']) && $result[$w][0]['endTimeStamp'] != "-1"){
									echo("<option value=\"" . (date("Y") + $i ). "\" selected>" . (date("Y") + $i ) . "</option>\n");
								}else{
									echo("<option value=\"" . (date("Y") + $i ). "\">" . (date("Y") + $i ) . "</option>\n");
								}
						}
						echo("</SELECT> \n");
				
				
				
				echo "&nbsp; (optional)
				
				
				
				</TD>
			</TR>
			
			<TR class=\"even\">
				<TD class=\"formDescriptiveText\">Days of Week:</TD>
				<TD class=\"formElementTextLeft\">";
				
				//#####################################
				// Form Item - DAYS
				//#####################################
				if($result[$w][0]['mon'] == 'true'){
					echo "mon:<INPUT name=\"mon$w\" type=\"checkbox\" checked value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
					echo "mon:<INPUT name=\"mon$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				
				if($result[$w][0]['tue'] == 'true'){
					echo "tue:<INPUT name=\"tue$w\" type=\"checkbox\" checked value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
					echo "tue:<INPUT name=\"tue$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				
				if($result[$w][0]['wed'] == 'true'){
					echo "wed:<INPUT name=\"wed$w\" type=\"checkbox\" checked value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
					echo "wed:<INPUT name=\"wed$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				if($result[$w][0]['thu'] == 'true'){
					echo "thu:<INPUT name=\"thu$w\" type=\"checkbox\" value=\"true\" checked onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
					echo "thu:<INPUT name=\"thu$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				if($result[$w][0]['fri'] == 'true'){
					echo "fri:<INPUT name=\"fri$w\" type=\"checkbox\" value=\"true\" checked onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
					echo "fri:<INPUT name=\"fri$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				if($result[$w][0]['sat'] == 'true'){
				echo "sat:<INPUT name=\"sat$w\" type=\"checkbox\" value=\"true\" checked onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
				echo "sat:<INPUT name=\"sat$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				
				if($result[$w][0]['sun'] == 'true'){
					echo "sun:<INPUT name=\"sun$w\" type=\"checkbox\" value=\"true\" checked onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}else{
					echo "sun:<INPUT name=\"sun$w\" type=\"checkbox\" value=\"true\" onkeypress=\"return swallowEnter(event,this.form)\">  &nbsp;&nbsp;";
				}
				echo"				
				</TD>
			</TR>
				
			<TR class=\"odd\">
				<TD class=\"formDescriptiveText\">Source:</TD>
				<TD class=\"formElementTextLeft\">";
				
				echo"
				<!-- JAVASCRIPT FOR SHOWING \"OTHER\" TEXT FIELD-->
				<script language=\"JavaScript\">
				<!--
				function showSourceOther$w(){
					if(document.form.source$w.value == 'Other'){
						document.getElementById(\"source_other$w\").style.visibility = 'visible';
					}else{
						document.getElementById(\"source_other$w\").style.visibility = 'hidden';
					}
				}
				
				
				
				
				
				function showOther$w(){
					if(document.form.type$w.value == 'Other'){
						document.getElementById(\"type_other$w\").style.visibility = 'visible';
					}else{
						document.getElementById(\"type_other$w\").style.visibility = 'hidden';
					}
				}
	
				//-->
				</script>
				
				";
				
				//#####################################
				// Form Item - SOURCE
				//#####################################
				//Source Drop Down List
				echo("<select name=\"source$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\" onChange=\"showSourceOther$w()\">\n");
				
				if($result[$w][0]['source'] == "&nbsp;"){
						echo("<option value=\"\" selected>Blank Entry</option>\n");
					}
					
				for ($i = 0; $i < (count($ProgramSources)/2); $i++) {
				
					if($result[$w][0]['source'] == $ProgramSources[($i + $i)]){
						echo("<option value=\"" . $ProgramSources[($i + $i)] . "\" selected>" . $ProgramSources[($i + $i + 1)] . "</option>\n");
					}else{
						echo("<option value=\"" . $ProgramSources[($i + $i)] . "\">" . $ProgramSources[($i + $i + 1)] . "</option>\n");
					}
				}
				echo("</SELECT>\n\n");
				
				if($result[$w][0]['source_other'] != ''){
					echo"&nbsp; <input id=\"source_other$w\" name=\"source_other$w\" type=\"text\" value=\"".stripslashes(stripslashes($result[$w][0]['source_other']))."\" size=\"23\" style=\"visibility:visible;\">";
				}

				echo"				
				</TD>
			</TR>				



			<TR class=\"even\">
				<TD class=\"formDescriptiveText\">Type:</TD>
				<TD class=\"formElementTextLeft\">";
				
				//#####################################
				// Form Item - Type
				//#####################################
				echo("<select name=\"type$w\" size=\"1\" onkeypress=\"return swallowEnter(event,this.form)\" onChange=\"showOther$w()\"> \n");
					if($result[$w][0]['type'] == "&nbsp"){
						echo("<option value=\"\" selected>Blank Entry</option>\n");
					}
				
				for ($i = 0; $i < (count($ProgramTypes)/2); $i++) {
					if($result[$w][0]['type'] == $ProgramTypes[($i + $i)]){
						echo("<option value=\"" . $ProgramTypes[($i + $i)] . "\" selected>" . $ProgramTypes[($i + $i + 1)] . "</option>\n");
					}else{
						echo("<option value=\"" . $ProgramTypes[($i + $i)] . "\">" . $ProgramTypes[($i + $i + 1)] . "</option>\n");
					}
				}
				echo("</SELECT>\n\n");
				if($result[$w][0]['type_other'] != ''){
					echo"&nbsp; <input id=\"type_other$w\" name=\"type_other$w\" type=\"text\" value=\"".stripslashes(stripslashes($result[$w][0]['type_other']))."\" size=\"23\" style=\"visibility:visible;\">";
				}				
				echo"					
				</TD>
			</TR>

				
		</TABLE>";
			
			
			
			//END LOOP
			}
			

		 
		 
		 }//end else
	 
		?>
		
		
		</TD>
	</TR>				
</TABLE>
<?PHP
if(isset($_GET['programID']) || count($_SESSION['updateArray']) > '0'){
 echo" <INPUT name=\"highNumber\" type=\"hidden\" value=\"".count($_SESSION['updateArray'])."\"> <INPUT name=\"action\" type=\"hidden\" value=\"updatePrograms\"><INPUT name=\"Submit\" type=\"button\" class=\"inputButton\"  value=\"Submit\" style=\"margin-bottom:5px; margin-top:5px;\" onClick=\"formCheck()\"> &nbsp; &nbsp; &nbsp; <INPUT name=\"Reset\" type=\"reset\" class=\"inputButton\" value=\"Reset\" style=\"margin-bottom:5px; margin-top:5px;\">";
}
//unset($_SESSION['updateArray']);
if(isset($_GET['fromLog'])){//send it back to daily Log
	echo" <INPUT name=\"fromLog\" type=\"hidden\" value=\"true\"><INPUT name=\"anchor\" type=\"hidden\" value=\"".$_GET['anchor']."\">";
}

?> 
</FORM>		
	</TD></TR>
</TABLE>

